package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjSmsReceiveRole;
import com.zb.entity.ZjSmsReceiveRoleEmp;
import com.zb.mapper.ZjSmsReceiveRoleMapper;
import com.zb.service.ZjSmsReceiveRoleEmpService;
import com.zb.service.ZjSmsReceiveRoleService;
import com.zb.vo.SmsReceiveRoleVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商家短信接收角色表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-05-22
 */
@Service
public class ZjSmsReceiveRoleServiceImpl extends ServiceImpl<ZjSmsReceiveRoleMapper, ZjSmsReceiveRole> implements ZjSmsReceiveRoleService {
    @Resource
    private ZjSmsReceiveRoleMapper smsReceiveRoleMapper;
    @Resource
    private ZjSmsReceiveRoleEmpService smsReceiveRoleEmpService;

    // 保存短信接收者类型
    @Override
    @Transactional
    public void save(Long custAccId, Long smsTemplateId, SmsReceiveRoleVo receiveRoleVo) {
        smsReceiveRoleMapper.deleteBySmsTemplateId(smsTemplateId);
        smsReceiveRoleEmpService.deleteBySmsTemplateId(smsTemplateId);
        if (CollectionUtil.isEmpty(receiveRoleVo.getRoleTypes())) {
            return;
        }
        if (receiveRoleVo.getRoleTypes().stream().anyMatch(f -> f == 2)) {
            Assert.notEmpty(receiveRoleVo.getEmpIds(), "接收者类型中包含有[指定人员]，必须选择接收者人员");
        }
        List<ZjSmsReceiveRole> receiveRoles = new ArrayList<>();
        ZjSmsReceiveRole receiveRole = null;
        List<ZjSmsReceiveRoleEmp> roleEmps = new ArrayList<>();
        ZjSmsReceiveRoleEmp emp = null;
        for (Integer roleType : receiveRoleVo.getRoleTypes()) {
            receiveRole = new ZjSmsReceiveRole();
            receiveRole.setId(IdWorker.getId());
            receiveRole.setCustAccId(custAccId);
            receiveRole.setSmsTemplateId(smsTemplateId);
            receiveRole.setRoleType(roleType);
            receiveRoles.add(receiveRole);
            if (roleType == 2) {
                for (Long empId : receiveRoleVo.getEmpIds()) {
                    emp = new ZjSmsReceiveRoleEmp();
                    emp.setId(IdWorker.getId());
                    emp.setCustAccId(custAccId);
                    emp.setSmsTemplateId(smsTemplateId);
                    emp.setSmsReceiveRoleId(receiveRole.getId());
                    emp.setEmpId(empId);
                    roleEmps.add(emp);
                }
            }
        }
        this.saveBatch(receiveRoles);
        if (CollectionUtil.isNotEmpty(roleEmps)) smsReceiveRoleEmpService.saveBatch(roleEmps);
    }

    // 根据短信模板ID获取接收者角色类型
    @Override
    public List<ZjSmsReceiveRole> getRoleTypeBySmsTemplateIds(List<Long> smsTemplateIds) {
        if (CollectionUtil.isEmpty(smsTemplateIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjSmsReceiveRole> wrapper = new QueryWrapper<>();
        wrapper.select("id", "sms_template_id", "role_type")
                .in("sms_template_id", smsTemplateIds);
        return this.list(wrapper);
    }

    // 根据短信模板ID获取短信接收角色类型
    @Override
    public SmsReceiveRoleVo getRoleTypeBySmsTemplateId(Long smsTemplateId) {
        QueryWrapper<ZjSmsReceiveRole> wrapper = new QueryWrapper<>();
        wrapper.select("id", "sms_template_id", "role_type")
                .eq("sms_template_id", smsTemplateId);
        List<ZjSmsReceiveRole> roleList = this.list(wrapper);
        SmsReceiveRoleVo roleVo = new SmsReceiveRoleVo();
        if (CollectionUtil.isEmpty(roleList)) {
            return roleVo;
        }
        roleVo.setRoleTypes(roleList.stream().map(ZjSmsReceiveRole::getRoleType).toList());
        if (roleVo.getRoleTypes().stream().anyMatch(f -> f == 2)) {
            List<ZjSmsReceiveRoleEmp> roleEmps = smsReceiveRoleEmpService.getBySmsTemplateId(smsTemplateId);
            roleVo.setEmpIds(roleEmps.stream().map(ZjSmsReceiveRoleEmp::getEmpId).toList());
        }
        return roleVo;
    }

    // 根据短信编号获取接收者角色手机号
    @Override
    public SmsReceiveRoleVo getRoleTypeBySmsCode(Long custAccId, String smsCode) {
        List<ZjSmsReceiveRole> receiveRoles = smsReceiveRoleMapper.getBySmsCode(custAccId, smsCode);
        SmsReceiveRoleVo receiveRoleVo = new SmsReceiveRoleVo();
        if (CollectionUtil.isEmpty(receiveRoles)) {
            return receiveRoleVo;
        }
        receiveRoleVo.setRoleTypes(receiveRoles.stream().map(ZjSmsReceiveRole::getRoleType).toList());
        if (receiveRoleVo.getRoleTypes().stream().anyMatch(f -> f == 2)) {
            receiveRoleVo.setPhones(smsReceiveRoleEmpService.getEmpPhoneBySmsTemplateId(receiveRoles.get(0).getSmsTemplateId()));
        }
        return receiveRoleVo;
    }
}
